from django.shortcuts import render

# Create your views here.
from rest_framework import generics
from rest_framework import mixins
from rest_framework import status
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView

from axf.models import Wheel, MainShow, FoodTypes, Goods, UserModel
from project.serializers import WheelSerializer, MainShowSerializer, FoodTypeSerializer, ShopListSerializer


class WheelList(APIView):
    """
    LC
    """
    def get(self, request, format=None):
        snippets = Wheel.objects.all()
        serializer = WheelSerializer(snippets, many=True)
        return Response(serializer.data)

class MainShow(mixins.ListModelMixin,
                mixins.CreateModelMixin,
                generics.GenericAPIView):
    queryset = MainShow.objects.all()
    serializer_class = MainShowSerializer
    def post(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

class FoodType(mixins.ListModelMixin, generics.GenericAPIView):
    queryset = FoodTypes.objects.all()
    serializer_class = FoodTypeSerializer
    def post(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

class ShopList(APIView):
    def post(self, request, format=None):
        categoryid = request.data.get('categoryid')
        cid = request.data.get('cid')
        order_rule = int(request.data.get('num'))
        print(order_rule == '3')
        print(categoryid,cid)
        if not cid or cid == '0':
            snippets = Goods.objects.filter(categoryid=categoryid)
        else:
            snippets = Goods.objects.filter(categoryid = categoryid ).filter(childcid = cid)

        if order_rule == 1:
            # 销量排序
            snippets = snippets.order_by("-productnum")
        elif order_rule == 2:
            # 价格升序
            snippets = snippets.order_by("-price")
        elif order_rule == 3:
            # 价格降序
            print('aaa')
            snippets = snippets.order_by("price")
        else:
            pass

        serializer = ShopListSerializer(snippets, many=True)
        return Response(serializer.data)

class Register(APIView):
    def post(self, request, format=None):
        # print('aaaaa')
        # print(request.data)
        username = request.data.get("username")
        password = request.data.get("userpwd")
        email = request.data.get("useremail")
        icon = request.data.get("userfile")

        print(password)

        user = UserModel()
        user.username = username
        user.password = password
        user.email = email
        user.icon = icon

        user.save()
        #
        # request.session['user_id'] = user.id
        return Response('ok')
